{% extends "base.html" %}

{% load humanize %}
{% load syntax %}
{% load person %}
{% load patch %}

{% block title %}{{submission.name}}{% endblock %}

{% block body %}
<script>
function toggle_div(link_id, headers_id, label_show, label_hide)
{
    var link = document.getElementById(link_id)
    var headers = document.getElementById(headers_id)

    var hidden = headers.style['display'] == 'none';

    if (hidden) {
        link.innerHTML = label_hide || 'hide';
        headers.style['display'] = 'block';
    } else {
        link.innerHTML = label_show || 'show';
        headers.style['display'] = 'none';
    }

}
</script>

<div>
  {% include "patchwork/partials/download-buttons.html" %}
  <h1>{{ submission.name }}</h1>
</div>

<table class="patchmeta">
 <tr>
  <th>Message ID</th>
  {% if submission.list_archive_url %}
  <td>{{ submission.url_msgid }} (<a href="{{ submission.list_archive_url }}">mailing list archive</a>)</td>
  {% else %}
  <td>{{ submission.url_msgid }}</td>
  {% endif %}
 </tr>
{% if submission.state %}
 <tr>
  <th>State</th>
  <td>{{ submission.state.name }}{% if submission.archived %}, archived{% endif %}</td>
 </tr>
{% endif %}
{% if submission.commit_ref %}
 <tr>
  <th>Commit</th>
  <td>{{ submission|patch_commit_display }}</td>
 </tr>
{% endif %}
{% if submission.delegate %}
 <tr>
  <th>Delegated to:</th>
  <td>{{ submission.delegate.profile.name }}</td>
 </tr>
{% endif %}
 <tr>
  <th>Headers</th>
  <td><a id="togglepatchheaders"
   href="javascript:toggle_div('togglepatchheaders', 'patchheaders')"
   >show</a>
   <div id="patchheaders" class="patchheaders" style="display:none;">
    <pre>{{submission.headers}}</pre>
   </div>
  </td>
 </tr>
{% if submission.series %}
 <tr>
  <th>Series</th>
  <td>
   <a href="{% url 'patch-list' project_id=project.linkname %}?series={{ submission.series.id }}">
    {{ submission.series.name }}
   </a> |
   <a id="togglepatchseries"
      href="javascript:toggle_div('togglepatchseries', 'patchseries', 'expand', 'collapse')"
   >expand</a>
   <div id="patchseries" class="submissionlist" style="display:none;">
    <ul>
     {% with submission.series.cover_letter as cover %}
      <li>
      {% if cover %}
       {% if cover == submission %}
        {{ cover.name|default:"[no subject]"|truncatechars:100 }}
       {% else %}
       <a href="{% url 'cover-detail' project_id=project.linkname msgid=cover.url_msgid %}">
        {{ cover.name|default:"[no subject]"|truncatechars:100 }}
       </a>
       {% endif %}
      {% endif %}
      </li>
     {% endwith %}
     {% for sibling in submission.series.patches.all %}
      <li>
       {% if sibling == submission %}
        {{ sibling.name|default:"[no subject]"|truncatechars:100 }}
       {% else %}
       <a href="{% url 'patch-detail' project_id=project.linkname msgid=sibling.url_msgid %}">
        {{ sibling.name|default:"[no subject]"|truncatechars:100 }}
       </a>
       {% endif %}
      </li>
     {% endfor %}
    </ul>
   </div>
  </td>
 </tr>
{% endif %}
{% if submission.related %}
 <tr>
  <th>Related</th>
  <td>
   <a id="togglerelated"
      href="javascript:toggle_div('togglerelated', 'related')"
   >show</a>
   <div id="related" class="submissionlist" style="display:none;">
    <ul>
     {% for sibling in related_same_project %}
      <li>
       {% if sibling.id != submission.id %}
        <a href="{% url 'patch-detail' project_id=project.linkname msgid=sibling.url_msgid %}">
         {{ sibling.name|default:"[no subject]"|truncatechars:100 }}
        </a>
       {% endif %}
      </li>
     {% endfor %}
     {% if related_different_project %}
      <a id="togglerelatedoutside"
         href="javascript:toggle_div('togglerelatedoutside', 'relatedoutside', 'show from other projects')"
      >show from other projects</a>
      <div id="relatedoutside" class="submissionlist" style="display:none;">
       {% for sibling in related_outside %}
        <li>
         <a href="{% url 'patch-detail' project_id=sibling.project.linkname msgid=sibling.url_msgid %}">
          {{ sibling.name|default:"[no subject]"|truncatechars:100 }}
         </a> (in {{ sibling.project }})
        </li>
       {% endfor %}
      </div>
     {% endif %}
    </ul>
   </div>
  </td>
 </tr>
{% endif %}
</table>

<div class="patchforms">
{% if patchform %}
 <div class="patchform patchform-properties">
  <h3>Patch Properties</h3>
   <form method="post">
    {% csrf_token %}
    <table class="form">
     <tr>
      <th>Change state:</th>
      <td>
       {{ patchform.state }}
       {{ patchform.state.errors }}
      </td>
     </tr>
     <tr>
      <th>Delegate to:</th>
      <td>
       {{ patchform.delegate }}
       {{ patchform.delegate.errors }}
      </td>
     </tr>
     <tr>
      <th>Archived:</th>
      <td>
       {{ patchform.archived }}
       {{ patchform.archived.errors }}
      </td>
     </tr>
     <tr>
      <td></td>
      <td>
       <input type="submit" value="Update">
      </td>
     </tr>
    </table>
  </form>
 </div>
{% endif %}

{% if createbundleform %}
 <div class="patchform patchform-bundle">
  <h3>Bundling</h3>
   <table class="form">
    <tr>
     <td>Create bundle:</td>
     <td>
       {% if createbundleform.non_field_errors %}
       <dd class="errors">{{createbundleform.non_field_errors}}</dd>
       {% endif %}
      <form method="post">
       {% csrf_token %}
       <input type="hidden" name="action" value="createbundle"/>
       {% if createbundleform.name.errors %}
       <dd class="errors">{{createbundleform.name.errors}}</dd>
       {% endif %}
        {{ createbundleform.name }}
       <input value="Create" type="submit"/>
      </form>
      </td>
    </tr>
{% if bundles %}
    <tr>
     <td>Add to bundle:</td>
     <td>
      <form method="post">
       {% csrf_token %}
       <input type="hidden" name="action" value="addtobundle"/>
       <select name="bundle_id"/>
        {% for bundle in bundles %}
         <option value="{{bundle.id}}">{{bundle.name}}</option>
        {% endfor %}
        </select>
       <input value="Add" type="submit"/>
      </form>
     </td>
    </tr>
{% endif %}
   </table>

 </div>
{% endif %}

 <div style="clear: both;">
 </div>
</div>

{% if submission.pull_url %}
<h2>Pull-request</h2>
<a class="patch-pull-url" href="{{submission.pull_url}}"
 >{{ submission.pull_url }}</a>
{% endif %}

{% if checks %}
<h2>Checks</h2>
<table class="checks">
<tr>
  <th>Context</th>
  <th>Check</th>
  <th>Description</th>
</tr>
{% for check in checks %}
<tr>
  <td>{{ check.user }}/{{ check.context }}</td>
  <td>
    <span title="Updated {{ check.date|naturaltime }}"
        class="state {{ check.get_state_display }}">
      {{ check.get_state_display }}
    </span>
  </td>
  <td>
    {% if check.target_url %}
    <a href="{{ check.target_url }}">
    {% endif %}
      {{ check.description }}
    {% if check.target_url %}
    </a>
    {% endif %}
  </td>
</tr>
{% endfor %}
</table>
{% endif %}

{% if submission.diff %}
<h2>Commit Message</h2>
{% else %}
<h2>Message</h2>
{% endif %}
<div class="comment">
<div class="meta">
 <span>{{ submission.submitter|personify:project }}</span>
 <span class="pull-right">{{ submission.date }} UTC</span>
</div>
<pre class="content">
{{ submission|commentsyntax }}
</pre>
</div>

{% for item in comments %}
{% if forloop.first %}
<h2>Comments</h2>
{% endif %}

<a name="{{ item.id }}"></a>
<div class="comment">
<div class="meta">
 <span>{{ item.submitter|personify:project }}</span>
 <span class="pull-right">{{ item.date }} UTC | <a href="{% url 'comment-redirect' comment_id=item.id %}"
   >#{{ forloop.counter }}</a></span>
</div>
<pre class="content">
{{ item|commentsyntax }}
</pre>
</div>
{% endfor %}

{% if submission.diff %}
<div>
  {% include "patchwork/partials/download-buttons.html" %}
  <h2>Patch</h2>
</div>
<div id="patch" class="patch">
<pre class="content">
{{ submission|patchsyntax }}
</pre>
</div>
{% endif %}

{% endblock %}
